package com.leo.mybatis.mapper;

import com.leo.mybatis.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;


@CacheNamespace(implementation = PerpetualCache.class)//开启二级缓存
public interface IUserMapper {

    //添加用户
    @Insert("insert into user(id,username) values(#{id},#{username})")
    void addUser(User user);

    //更新用户
    @Update("update user set username = #{username} where id = #{id}")
    void updateUser(User user);

    //查询用户
    @Select("select * from user")
    List<User> selectUser();

    //删除用户
    @Delete("delete from user where id = #{id}")
    void deleteUser(Integer id);


    @Select({"select * from user where id = #{id}"})
    User findUserById(Integer id);


    //一对多
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "com.leo.mybatis.mapper.IOrderMapper.findOrderByUid"))
    })
    List<User> findAll();

    //多对多
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "com.leo.mybatis.mapper.IRoleMapper.findRoleByUid"))
    })
    List<User> findAllUserAndRole();

}
